<?php

namespace app\common\model;

use DateTime;
use think\Model;

/**
 * 会员余额日志模型
 */
class MoneyLog extends Model
{

    // 表名
    protected $name = 'user_money_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
        'source_text',
        'createtime_text',

    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getTypeList()
    {
        return ['1' => __('支出'), '2' => __('收入')];
    }
    public function getSourceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['source_type']) ? $data['source_type'] : '');
        $list = $this->getSourceType();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getSourceType($num = 0)
    {
        return [
            100 => '商品订单结算共【' . $num . '】单，获得佣金',
            101 => '月度' . $num . '奖励金额',
            200 => '管理员变更金额',
            500 => '提现扣减佣金',
            501 => '拒绝提现返还佣金',
        ];
    }

    public function getList($type, $status = 0, $user_id)
    {
        $log = new self;
        if ($type == 2) {
            if (isset($status) && !empty($status)) {
                if ($status == 1) {
                    $log->where('source_type', '100');
                } else {
                    $log->where('source_type', '101');
                }
            }
        }
        return $log->where('type', $type)->where('user_id', $user_id)->order('id desc')->paginate(10);
    }
    public function getLists($date, $user_id)
    {
        $log = new self;
        $endDate = new DateTime($date);
        $startDate = $endDate->format('Y-m-d');
        $endDate = $endDate->format('Y-m-t') . ' 23:59:59';
        return $log->where('type', 2)->where('user_id', $user_id)->whereTime('createtime', 'between', [$startDate, $endDate])->order('id desc')->select();
    }

    /**
     * Notes:记录会员账户流水，如果变动类型是成长值，且是增加的，则调用更新会员等级方法。该方法应在添加用户账户后调用
     * @author: cjh 2020/12/15 11:49
     * @param int $user_id  用户id
     * @param float $amount 变动数量
     * @param int $change_type  变动类型：1-减少；2-增加
     * @param int $source_type 来源类型
     * @param string $source_id 来源id
     * @param string $source_sn 来源单号
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function income($user_id, $amount, $change_type, $source_type, $source_sn = '', $num = 0)
    {
        $user = new User;
        $user = $user->get($user_id);
        if (empty($user)) {
            return false;
        }
        $before = $user->money ?? 0;
        if ($change_type == 1) {
            $user->setDec('money', $amount);
            $amount = -$amount;
        } else {
            $user->setInc('money', $amount);
        }
        $list =  $this->getSourceType($num);
        $left_amount = $user->money;
        $account_log = new self;
        $data['type'] = $change_type;
        $data['order_sn'] = $source_sn;
        $data['user_id'] = $user_id;
        $data['money'] = $amount ?? 0;
        $data['num'] = $num ?? 0;
        $data['before'] = $before ?? 0;
        $data['after'] = $left_amount ?? 0;
        $data['memo'] = isset($list[$source_type]) ? $list[$source_type] : '';
        $data['source_type'] = $source_type;
        $account_log->allowField(true)->save($data);

        return true;
    }
}
